package com.lyq.controller;

import com.lyq.mapper.DoctorMapper;
import com.lyq.mapper.OrderMapper;
import com.lyq.pojo.Doctor;
import com.lyq.pojo.Order;
import com.lyq.untis.UntisMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyq
 * @since 2020-05-29
 */
@RestController
@RequestMapping("/weixing/order")
public class OrderController {
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private OrderMapper orderMapper;
    @RequestMapping(value="/insertOrder",method = RequestMethod.POST)
    @ResponseBody
    public UntisMsg insertOrder(@RequestBody Order order ){
        System.out.println(order);
        int did = order.getOrderDoId();
        int insert = orderMapper.insert(order);
        UntisMsg msg = new UntisMsg();
        int amount= 0;
        if(insert>0){
            Doctor doctor = doctorMapper.selectById(did);
             amount=(doctor.getAmount()-1);
               doctor.setAmount(amount);
            int updateById = doctorMapper.updateById(doctor);
            // 预约成功
                msg.setResult(true);
                msg.setMsg("约成功啦 ");
                return msg;

        }else{
            // 预约失败
            msg.setResult(false);
            msg.setMsg("约失败 ");
            return msg;

        }

    }
    @RequestMapping("/selectOrderByName")
    public List<Order> selectOrderByName(String nickName){
         Map<String,Object> map = new HashMap<>();
           map.put("nick_name",nickName);
        List<Order> orders = orderMapper.selectByMap(map);
        System.out.println(orders);
        return orders;
    }
    @RequestMapping("/selectOrderById")
    public Order selectOrderById(int id){
        Order orderone = orderMapper.selectById(id);


        return orderone;
    }
    @RequestMapping("/updateOrderByid")
    public UntisMsg updateOrderByid(int id, int orderCondition){
        Order order = orderMapper.selectById(id);
        if(orderCondition==1){
            order.setOrderCondition(0);
        }else {
            order.setOrderCondition(1);
        }
        int i = orderMapper.updateById(order);
        UntisMsg msg = new UntisMsg();
        msg.setResult(true);
        msg.setMsg("变跟成功 ");
        return msg;

    }



}

